package com.vision.sales.sv.serv.rpc;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.vision.core.cm.data.TaskNode;
import com.vision.core.cm.data.TaskNode.NodeType;
import com.vision.core.cm.db.data.Permission;
import com.vision.core.cm.exception.RpcException;
import com.vision.core.cm.serv.rpc.MenuService;
import com.vision.core.sv.serv.BaseRpcServlet;
import com.vision.sales.cl.SalesTasks;
import com.vision.sales.cm.data.SalesPermissions;

/**
 * 
 * @author Mark
 * 
 */
@Service("menu")
public class MenuServiceImpl extends BaseRpcServlet implements MenuService {

	private static final long serialVersionUID = 1L;
	
	public MenuServiceImpl() {
		initGilead();
	}

	@Override
	public List<TaskNode> getMenu() throws RpcException {
		Set<Permission> permissions = getLoggedInUsersPermissions();
		
		List<TaskNode> list = new ArrayList<TaskNode>();
		
		TaskNode masterlist = new TaskNode(SalesTasks.MASTERLIST);
		addIf(masterlist, SalesTasks.PRODUCTS, permissions, SalesPermissions.ViewProduct);
		addIf(masterlist, SalesTasks.SALESMEN, permissions, SalesPermissions.ViewSalesman);
		addIf(masterlist, SalesTasks.CUSTOMERS, permissions, SalesPermissions.ViewCustomer);
		addIf(masterlist, SalesTasks.CHECKS, permissions, SalesPermissions.ViewCheck);
		addIf(masterlist, SalesTasks.MONTHLY_TOTALS, permissions, SalesPermissions.ViewMonthlyTotal);
		
		if (masterlist.hasChildren()) {
//			list.addAll(masterlist.getChildren());
			list.add(masterlist);
		}
		
		TaskNode transactions = new TaskNode(SalesTasks.TRANSACTIONS);
		addIf(transactions, SalesTasks.DELIVERY_RECEIPTS, permissions, SalesPermissions.ViewDeliveryReceipt, NodeType.BUTTON);
		addIf(transactions, SalesTasks.SALES_INVOICES, permissions, SalesPermissions.ViewSalesInvoice, NodeType.BUTTON);
		addIf(transactions, SalesTasks.COLLECTIONS, permissions, SalesPermissions.ViewCollection, NodeType.BUTTON);
		addIf(transactions, SalesTasks.CREDIT_MEMOS, permissions, SalesPermissions.ViewCreditMemo, NodeType.BUTTON);
		addIf(transactions, SalesTasks.DEBIT_MEMOS, permissions, SalesPermissions.ViewDebitMemo, NodeType.BUTTON);
		addIf(transactions, SalesTasks.ADVANCE_PAYMENTS, permissions, SalesPermissions.ViewAdvancePayment, NodeType.BUTTON);
		
		if (transactions.hasChildren()) {
			list.addAll(transactions.getChildren());
//			list.add(transactions);
		}
		
		TaskNode reports = new TaskNode(SalesTasks.REPORTS);
		addIf(reports, SalesTasks.CUSTOMER_LEDGER, permissions, SalesPermissions.ViewCustomerLedgerReport);
		addIf(reports, SalesTasks.CUSTOMER_SALES, permissions, SalesPermissions.ViewCustomerSalesReport);
		addIf(reports, SalesTasks.PRODUCT_SALES, permissions, SalesPermissions.ViewProductSalesReport);
//		addIf(reports, SalesTasks.MONTHLY_GROUP_SALES, permissions, SalesPermissions.ViewMonthlyGroupSalesReport);
		addIf(reports, SalesTasks.SALESMAN_SALES, permissions, SalesPermissions.ViewSalesmanSalesReport);
		addIf(reports, SalesTasks.INDIV_SALESMAN_SALES, permissions, SalesPermissions.ViewIndividualSalesmanSalesReport);
		addIf(reports, SalesTasks.INDIV_SALESMAN_COMM, permissions, SalesPermissions.ViewIndividualSalesmanCommissionReport);
		addIf(reports, SalesTasks.OUTSTANDING_RECEIVABLES, permissions, SalesPermissions.ViewOutstandingReceivablesReport);
		addIf(reports, SalesTasks.DELIVERY_RECEIPTS_REPORT, permissions, SalesPermissions.ViewDeliveryReceiptsReport);
		addIf(reports, SalesTasks.INDIVIDUAL_SOA, permissions, SalesPermissions.ViewIndividualSOAReport);
		addIf(reports, SalesTasks.CUSTOMER_AGING_ANALYSIS, permissions, SalesPermissions.ViewCustomerAgingReport);
		addIf(reports, SalesTasks.COMMISSION_REPORT, permissions, SalesPermissions.ViewCommissionReport);
		
		if (reports.hasChildren())
			list.add(reports);

		return list;
	}
	
	private static void addIf(TaskNode node, TaskNode task, Set<Permission> permissions, Permission p) {
		if (permissions.contains(p))
			node.addChildren(new TaskNode(task));
	}
	
	private static void addIf(TaskNode node, TaskNode task, Set<Permission> permissions, Permission p, NodeType type) {
		if (permissions.contains(p))
			node.addChildren(new TaskNode(task, type));
	}

}
